/**
 * @fileoverview Tests for no-native-reassign rule.
 * @author Ilya Volodin
 * @deprecated in ESLint v3.3.0
 */

"use strict";

//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------

const rule = require("../../../lib/rules/no-native-reassign"),
	RuleTester = require("../../../lib/rule-tester/rule-tester"),
	globals = require("globals");

//------------------------------------------------------------------------------
// Tests
//------------------------------------------------------------------------------

const ruleTester = new RuleTester();

ruleTester.run("no-native-reassign", rule, {
	valid: [
		"string = 'hello world';",
		"var string;",
		{ code: "Object = 0;", options: [{ exceptions: ["Object"] }] },
		"top = 0;",
		{ code: "onload = 0;", languageOptions: { globals: globals.browser } },
		"require = 0;",
		{ code: "a = 1", languageOptions: { globals: { a: true } } },
		"/*global a:true*/ a = 1",
	],
	invalid: [
		{
			code: "String = 'hello world';",
			errors: [
				{
					messageId: "nativeReassign",
					data: { name: "String" },
				},
			],
		},
		{
			code: "String++;",
			errors: [
				{
					messageId: "nativeReassign",
					data: { name: "String" },
				},
			],
		},
		{
			code: "({Object = 0, String = 0} = {});",
			languageOptions: { ecmaVersion: 6 },
			errors: [
				{
					messageId: "nativeReassign",
					data: { name: "Object" },
				},
				{
					messageId: "nativeReassign",
					data: { name: "String" },
				},
			],
		},
		{
			code: "top = 0;",
			languageOptions: { globals: globals.browser },
			errors: [
				{
					messageId: "nativeReassign",
					data: { name: "top" },
				},
			],
		},
		{
			code: "require = 0;",
			languageOptions: { sourceType: "commonjs" },
			errors: [
				{
					messageId: "nativeReassign",
					data: { name: "require" },
				},
			],
		},

		// Notifications of readonly are moved from no-undef: https://github.com/eslint/eslint/issues/4504
		{
			code: "/*global b:false*/ function f() { b = 1; }",
			errors: [
				{
					messageId: "nativeReassign",
					data: { name: "b" },
				},
			],
		},
		{
			code: "function f() { b = 1; }",
			languageOptions: { globals: { b: false } },
			errors: [
				{
					messageId: "nativeReassign",
					data: { name: "b" },
				},
			],
		},
		{
			code: "/*global b:false*/ function f() { b++; }",
			errors: [
				{
					messageId: "nativeReassign",
					data: { name: "b" },
				},
			],
		},
		{
			code: "/*global b*/ b = 1;",
			errors: [
				{
					messageId: "nativeReassign",
					data: { name: "b" },
				},
			],
		},
		{
			code: "Array = 1;",
			errors: [
				{
					messageId: "nativeReassign",
					data: { name: "Array" },
				},
			],
		},
	],
});
